﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class DiemRenLuyenTheoThang : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Thembtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.CommandText = "DiemRenLuyenThem";
                //cmd.Parameters.AddWithValue("@mahocvien", MaHocVientxt.Text);
                //cmd.Parameters.AddWithValue("@thang", Thangtxt.Text);
                //cmd.Parameters.AddWithValue("@nam", Namtxt.Text);
                //cmd.Parameters.AddWithValue("@diem", Diemtxt.Text);
                SqlParameter MHVpar = new SqlParameter("@mahocvien", SqlDbType.NVarChar, 20);
                SqlParameter Thangpar = new SqlParameter("@thang", SqlDbType.Int);
                SqlParameter Nampar = new SqlParameter("@nam", SqlDbType.Int);
                SqlParameter Diempar = new SqlParameter("@diem", SqlDbType.Float);
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into RenLuyenDiem(MaHocVien,Thang,Nam,Diem) values(@mahocvien, @thang, @nam,@diem)";
                cmd.Parameters.Add(MHVpar);
                MHVpar.Value = MaHocVientxt.Text;
                cmd.Parameters.Add(Thangpar);
                Thangpar.Value = Thangtxt.Text;
                cmd.Parameters.Add(Nampar);
                Nampar.Value = Namtxt.Text;
                cmd.Parameters.Add(Diempar);
                Diempar.Value = Diemtxt.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                DiemrenLuyendgv.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Suabtn_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow dong = DiemrenLuyendgv.SelectedRow;
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "RenLuyenSua";
                //cmd.CommandText = "DiemRenLuyenSua";
                //cmd.Parameters.AddWithValue("@stt", dong.Cells[1].Text);
                cmd.Parameters.AddWithValue("@mahocvien", MaHocVientxt.Text);
                cmd.Parameters.AddWithValue("@thang", Thangtxt.Text);
                cmd.Parameters.AddWithValue("@nam", Namtxt.Text);
                cmd.Parameters.AddWithValue("@diem", Diemtxt.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                DiemrenLuyendgv.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Xoabtn_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow dong = DiemrenLuyendgv.SelectedRow;
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "RenLuyenXoa";
                //cmd.CommandText = "DiemRenLuyenXoa";
                //cmd.Parameters.AddWithValue("@stt", dong.Cells[1].Text);
                cmd.Parameters.AddWithValue("@mahocvien", dong.Cells[1].Text);
                cmd.Parameters.AddWithValue("@thang", dong.Cells[3].Text);
                cmd.Parameters.AddWithValue("@nam", dong.Cells[4].Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                DiemrenLuyendgv.DataBind();
                Response.Write("Xóa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void DiemrenLuyendgv_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = DiemrenLuyendgv.SelectedRow;
            MaHocVientxt.Text= HttpUtility.HtmlDecode(dong.Cells[1].Text); 
            Thangtxt.Text = HttpUtility.HtmlDecode(dong.Cells[3].Text); 
            Namtxt.Text = HttpUtility.HtmlDecode(dong.Cells[4].Text);
            Diemtxt.Text = HttpUtility.HtmlDecode(dong.Cells[5].Text); 
        }
    }
}